package cn.wx.org.service.impl;


import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.org.domain.Employee;
import cn.wx.org.domain.Shop;
import cn.wx.org.domain.ShopLog;
import cn.wx.org.dto.ShopDto;
import cn.wx.org.mapper.EmployeeMapper;
import cn.wx.org.mapper.ShopLogMapper;
import cn.wx.org.mapper.ShopMapper;
import cn.wx.org.service.IEmployeeService;
import cn.wx.org.service.IShopService;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service

public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        // 实现shop入驻流程
        // 1. 非空校验  spring中提供了一个类，处理string的
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())

                || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getPhone())
                || StringUtils.isEmpty(shop.getEmployee().getEmail())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())
                || StringUtils.isEmpty(shop.getEmployee().getComfirmPassword())
        ) {
            // 提示店铺商家，参数不能为空
            throw new BusinessException("参数不能为空");
        }

        Employee admin = shop.getEmployee();
        // 2. 两次密码不一致校验
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("两次密码不一致，请重新输入！");
        }

        // 3. 店铺是否注册过校验
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if (dbShop != null) {
            throw new BusinessException("该店铺已经被注册过！");
        }


        // 把shop和employee数据保存在表中
        // 1. 保存employee, 获取自增的主键
        employeeMapper.save(admin);

        // 2. 把自增的主键设置到shop表中admin_id中
        shop.setAdmin_id(admin.getId());
        // 3. 保存shop数据，返回自增的主键
        shopMapper.save(shop);

        // 4. 3 返回的自增主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        // 5. 更新employee数据
        employeeMapper.update(admin);

    }

    /*
     *  审核驳回
     * */
    @Override
    public void reject(ShopLog log) throws MessagingException {   //log shop_id  note
        //1.修改shop状态
        //通过shop_id查询shop数据，在修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核驳回
        shop.setState(4);
        //修改数据库
        shopMapper.update(shop);
        //2.录入日志
        log.setState(2);
        log.setAudit_id(1L);
        shopLogMapper.save(log);
        //3.通知: 发邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2904766264@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1> <a href='http://localhost:8080/shop/active/"+ shop.getId()+"'>点击该链接激活</a>",true);

        //收件人
        //获取收件人 通过id
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
//        helper.setTo(employee.getEmail());
        helper.setTo("2904766264@qq.com");
        //发送
        javaMailSender.send(mimeMessage);


    }


    /*
     *  审核通过
     * */
    @Override
    public void pass(ShopLog log) throws MessagingException {
//1.修改shop状态
        //通过shop_id查询shop数据，在修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(2);
        //修改数据库
        shopMapper.update(shop);
        //2.录入日志
        log.setState(2);
        log.setAudit_id(1L);
        shopLogMapper.save(log);
        //3.通知: 发邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2904766264@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1> <a href='http://localhost:8080/shop/active/"+ shop.getId()+"'>点击该链接激活</a>",true);

        //收件人
        //获取收件人 通过id
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
//        helper.setTo(employee.getEmail());
        helper.setTo("2904766264@qq.com");
        //发送
        javaMailSender.send(mimeMessage);


    }

    @Override
    public List<ShopDto> getCountByState() {
      return   shopMapper.getCountByState();
    }
}